package com.huxiu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.huxiu.fragments.ContentFragment;
import com.huxiu.utils.Constants;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.UmengUpdate;
import com.huxiu.utils.Utils;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;
import com.umeng.update.UmengUpdateAgent;

public class MainActivity extends BaseActivity {
    private static final String CONTENT_TAG = "content_tag";
    private long exitTime = 0;
    private UmengUpdate mUmengUpdate;
    private MainActivity instance;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置正文
        setContentView(R.layout.activity_main);
        //设置更新
        instance=this;
        UmengUpdate();
        initFragment();
        initWeibo();//qcl初始化微博
        String json = Utils.getDeviceInfo(this);
        PushAgent.getInstance(getApplicationContext()).onAppStart();
        String device_token = UmengRegistrar.getRegistrationId(getApplicationContext());
        Log.e("device_token","device_token"+device_token);


    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    static FragmentManager fragmentManager;

    private void initFragment() {
        // 获取Fragment管理器
        fragmentManager = getSupportFragmentManager();
        // 开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // ViewGroup.add(LeftFragment.onCreateView);
        transaction.replace(R.id.fl_content, new ContentFragment(), CONTENT_TAG);
        // 提交事务
        transaction.commit();
    }

    // 对外提供返回正文对象的方法
    public static ContentFragment getContentFragment() {
        return (ContentFragment) fragmentManager.findFragmentByTag(CONTENT_TAG);
    }


    //11-27新浪微博相关
    public AuthInfo mWeiboAuth;

    public static SsoHandler mSsoHandler;

    protected void initWeibo() {
        mWeiboAuth = new AuthInfo(this, Constants.SinaAppKey, Constants.SinaAppRedirectURI,
                Constants.SinaScope);
        mSsoHandler = new SsoHandler(this, mWeiboAuth);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mSsoHandler != null) {
            mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

    //    //微信相关
    //    public static IWXAPI mWechatApi;
    //    private void initWeixin() {
    //        mWechatApi = WXAPIFactory.createWXAPI(this, Constants.WECHAT_KEY);
    //    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
                return true;
            } else {
                MainActivity.this.finish();
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }


    public void UmengUpdate(){
        mUmengUpdate=new UmengUpdate(instance,true);
        UmengUpdateAgent.setUpdateAutoPopup(false);
        UmengUpdateAgent.setUpdateListener(mUmengUpdate);
        UmengUpdateAgent.update(this);
    }
}
